package com.web.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.pojo.Result;
import com.web.manager.pojo.Teacher;
import com.web.manager.pojo.User;
import com.web.manager.service.TeacherService;
import com.web.manager.exception.ServiceException;
import com.web.manager.utils.TokenUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;




    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Teacher teacher){

        try{
            teacherService.save(teacher);  //insertUser//save==>插入一个信息新增
        }catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误,用户名重复");
            } else {
                return Result.error("系统错误");
            }
        }

        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Teacher teacher){
        teacherService.updateById(teacher);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        User currentUser = TokenUtils.getCurrentUser();
        if (id.equals(currentUser.getId())) {
            throw new ServiceException("不能删除当前的用户");
        }
        teacherService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        teacherService.removeBatchByIds(ids);   //batchDeleteUser
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Teacher> userList = teacherService.list();  //selectAll
        return Result.success(userList);
    }



    //根据id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Teacher teacher= teacherService.getById(id);   //selectById
        return Result.success(teacher);
    }



    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String username,
                               @RequestParam String name) {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(username),"username",username);
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<Teacher> page = teacherService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }
}
